//package com.game.manager.security;
//
//import com.auth0.jwt.exceptions.JWTVerificationException;
//import com.game.manager.service.JwtTokenService;
//import lombok.AllArgsConstructor;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.SimpleGrantedAuthority;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@Component
//@AllArgsConstructor
//public class JwtTokenFilter extends OncePerRequestFilter {
//    private JwtTokenService jwtTokenService;
//    private final String TOKEN_HEADER = "game-token";
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request,
//                                    HttpServletResponse response,
//                                    FilterChain filterChain) throws ServletException, IOException {
//
//        String token = request.getHeader(TOKEN_HEADER);
//        if (token == null) {
//            filterChain.doFilter(request, response);
//            return;
//        }
//
//        try {
//            String username = jwtTokenService.getUsername(token);
//            List<? extends GrantedAuthority> authorities = jwtTokenService.getAuthorities(token)
//                    .stream()
//                    .map(SimpleGrantedAuthority::new)
//                    .collect(Collectors.toList());
//
//            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
//                    username,
//                    null,
//                    authorities
//            );
//            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//
//            filterChain.doFilter(request, response);
//        } catch (JWTVerificationException e) {
//            filterChain.doFilter(request, response);
//        }
//    }
//}
